import * as Lodash from 'lodash';
import * as echarts from 'echarts';

export const updateOptions = (options: echarts.EChartsOption, data: any[]) => {
  const opts = Lodash.cloneDeep(options);
  if (Lodash.isArray(opts.series)) {
    for (let i = 0; i < data.length; i += 1) {
      opts.series[i].data = data[i];
    }
  }
  // console.log(opts);
  return opts;
};

export const updateAxisData = (
  options: any,
  data: any[],
  axis: 'xAxis' | 'yAxis'
) => {
  const axisData: string[] = [];
  for (let i = 0; i < data.length; i += 1) {
    const item = data[i];
    axisData.push(item.name);
  }
  if (axis === 'xAxis' && options.xAxis && 'data' in options.xAxis) {
    options.xAxis.data = axisData;
  } else if (axis === 'yAxis' && options.yAxis && 'data' in options.yAxis) {
    options.yAxis.data = axisData;
  }
  return options;
};

export const updateGraphic = (
  graphics: any[],
  values: any[],
  color: '#FFF' | '#00F9FF'
) => {
  if (!Lodash.isArray(graphics)) {
    return graphics;
  }
  for (let i = 0; i < graphics.length; i += 1) {
    const graphic = graphics[i];
    if (graphic.style) {
      graphic.style.text = `${values[i]}`;
      graphic.style.fill = color;
    }
  }
  return graphics;
};
